package com.yc.jpyy.investig.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.base.util.PageBean;
import com.yc.jpyy.investig.dao.InvestigDao;
import com.yc.jpyy.investig.service.InvestigService;


@Service
public class InvestigServiceImpl extends BaseService implements InvestigService {
    
	@Autowired
	private InvestigDao investigDao;

	

	@Override
	public int addInvestigServer(Map<String, Object> InvestigInfo) throws DataAccessException {
		int ret = investigDao.addInvestigDao(InvestigInfo);
		System.out.println(ret);
		return ret;
	}

	@Override
	public int updateInvestigServer(Map<String, Object> InvestigInfo) throws DataAccessException {
		int ret = investigDao.updatePenaltyScore(InvestigInfo);
		return ret;	
	}
	
	@Override
	public int updateInvestig(Map<String, Object> InvestigInfo) throws DataAccessException {
		if(Optional.ofNullable(InvestigInfo.get("punishmentdate")).orElse("").toString().equals("")){
			InvestigInfo.put("punishmentdate",null);
		}
		int ret = investigDao.updateInvestigDao(InvestigInfo);
		return ret;	
	}

	@Override
	public int delInvestigServer(Map<String, Object> InvestigInfo) throws DataAccessException {
		int ret = investigDao.delInvestigDao(InvestigInfo);
		return ret;
	}
	
	

	@Override
	public Map<String, Object> findInvestigByIDServer(Map<String, Object> paramsMap) throws DataAccessException {
	     return investigDao.findInvestigByIdDao(paramsMap);
	}

	

	@Override
	public PageBean findInvestigServer(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		String ispunish = Optional.ofNullable(paramsMap.get("ispunish")).orElse("").toString();
		paramsMap.put("ispunish", ispunish.equals("")?"":ispunish.split(","));
		String yuliu2 = Optional.ofNullable(paramsMap.get("yuliu2")).orElse("").toString();
		paramsMap.put("yuliu2", yuliu2.equals("")?"":yuliu2.replaceAll("'", "").split(","));
		List<Map<String, Object>> list = investigDao.findInvestigDao(paramsMap);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findInvestigCountServer(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public int findInvestigCountServer(Map<String, Object> paramsMap) throws DataAccessException {
		return investigDao.findInvestigCountDao(paramsMap);
	}

	@Override
	public int delforUpdateInvestigServer(Map<String, Object> InvestigInfo) throws DataAccessException {
		return investigDao.delforUpdateInvestigDao(InvestigInfo);
	}

	@Override
	public PageBean findAccountReportInvestigServer(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		paramsMap.put("ispunish", Optional.ofNullable(paramsMap.get("ispunish")).orElse("").toString().split(","));
		List<Map<String, Object>> list = investigDao.findAccountReportInvestigDao(paramsMap);
		System.out.println("2222");
		System.out.println(list);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findInvestigCountServer(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public PageBean findWeeksReportInvestigServer(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		paramsMap.put("ispunish", Optional.ofNullable(paramsMap.get("ispunish")).orElse("").toString().split(","));
		List<Map<String, Object>> list = investigDao.findWeeksReportInvestigDao(paramsMap);
		System.out.println("2222");
		System.out.println(list);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findInvestigCountServer(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public List<Map<String, Object>> findPunishmentServer(Map<String, Object> paramsMap) throws DataAccessException {
		List<Map<String, Object>> list = investigDao.findPunishmentDao(paramsMap);
		return list;
	}

	@Override
	public int updateInvestigScoreToDriverAll(Map<String, Object> paramsMap) throws DataAccessException {
		int retValue = 0 ;
		retValue = investigDao.updatePenaltyScore(paramsMap);
		retValue =  investigDao.updateInvestigScoreToDriver(paramsMap);
		return retValue;
	}

	@Override
	public Map<String, Object> findDriverIDforInvestig(Map<String, Object> paramsMap) throws DataAccessException {
		// TODO Auto-generated method stub
		return investigDao.findDriverIDforInvestig(paramsMap) ;
	}

	@Override
	public Map<String, Object> getInvestigsByDriverId(
			Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		List<Map<String,Object>> investigList = investigDao.getInvestigsByDriverId(paramsMap);
		if(investigList != null && investigList.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData", investigList);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统查询不到数据");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> cancel(Map<String, Object> paramsMap)
			throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		String str  = investigDao.cancel(paramsMap);
		if(str != null && !str.equals("") && !str.isEmpty()){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData", str);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "撤回失败");
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updateInvesStatus(Map<String, Object> paramsMap)
			throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		paramsMap.put("idarray", paramsMap.get("ids").toString().split(","));
		int i = investigDao.updateInvesStatus(paramsMap);
		if(i > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("returnMsg", "更新成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "更新失败");
		}
		return resultMap;
	}

	/*@Override
	public Map<String, Object> findInvestigByVariable(
			Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		List<Map<String,Object>> list = investigDao.findInvestigByVariable(paramsMap);
		int counts = investigDao.findInvestigByVariableCount(paramsMap);
		if(list != null && list.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData",list);
			resultMap.put("TotalCount",counts);
			resultMap.put("TotalPage",counts % pagesize >0 ? counts/pagesize + 1 : counts/pagesize);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}*/

	
}
